// Copyright (C) 2008 Taylor L. Riche <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

#include <sys/stat.h>
#include <fcntl.h>
#include "lc_shallow2_op_Operator.h"
#define BUFFER_SIZE 65536


void 
shallow2_op::shallowInspect(requestTypes::NetRequest * data, unsigned long dataSize)
{
  //std::cerr << "IN SHALLOW" << std::endl;
  /*if(data->getFileName().length() > 30) {
    requestTypes::ControlMessage * cm = new requestTypes::ControlMessage(requestTypes::SUSPICIOUS, data->getSourceID());
    control_out(cm, sizeof(requestTypes::ControlMessage));
  }
  data_out(data, dataSize);*/
    int rd;
  struct stat sb;
  int res=0;
  socket_t dataSock = data->getSocketID();

  std::string root= "/var/local/badflow/appdata/";
  std::string file = data->getFileName();
  std::string content="text/plain";

  file = root + file;


  res = stat(file.c_str(), &sb);
  int file_size = sb.st_size;

  if (res < 0) {
    std::string msg1 = "HTTP/1.1 404 File not found\r\n";
    std::string msg;
    msg.append(msg1);
    msg.append("Content-Type: ");
    msg.append(content);
    msg.append("\r\nServer: lagniappe \r\nContent-Length: ");
    msg.append(boost::lexical_cast<std::string>(msg1.length()));
    msg.append("\r\n\r\n");
    char *bad_buffer = new char[msg.length()];

    strcpy(bad_buffer, msg.c_str());

    requestTypes::NetRequest *out_data = new requestTypes::NetRequest(bad_buffer,strlen(bad_buffer),data->getSocketID());
    assert(out_data != NULL);
    data_out(out_data, sizeof(requestTypes::NetRequest));
  }
  else {
    int ix=0;
    int header_len;

    char hdrs[BUFFER_SIZE];
    if (data->getClose()) {
      sprintf(hdrs, "HTTP/1.1 200 OK\r\nContent-type: %s\r\nServer: Lagniappe 0.1\r\nConnection: close\r\nContent-Length: %d\r\n\r\n",
              content.c_str(), file_size);
    }
    else {
      sprintf(hdrs, "HTTP/1.1 200 OK\r\nContent-type: %s\r\nServer: Lagniappe 0.1\r\nContent-Length: %d\r\n\r\n",
              content.c_str(), file_size);
    }
    header_len = strlen(hdrs);

    char read_data[BUFFER_SIZE];
    std::string read_string = "";
    for(int i = 0; i < file_size; ++i) {
       read_string += 'a';
    }
    char *new_buffer = new char[header_len + strlen(read_string.c_str())];
    strcpy(new_buffer, hdrs);
    strcat(new_buffer, read_string.c_str());
    requestTypes::NetRequest *out_data = new requestTypes::NetRequest(new_buffer,strlen(new_buffer), dataSock,data->getFileName());
    assert(out_data != NULL);
    assert(dataSock >= 0);
        assert(out_data->getSocketID()>= 0);
            data_out(out_data, sizeof(requestTypes::NetRequest));
              }
                if(!inProf) delete data;
    
                  return;
    
}

requestTypes::NetRequest *
shallow2_op::gendata_inRequest()
{
  //std::string request = "GET /index.html HTTP/1.1\r\n\r\n";
  //request += "\r\n\r\n";
  //char * request_str = new char[request.length()];
  //strcpy(request_str, request.c_str());
  //std::cerr << request_str << ":" << strlen(request_str) << ":" << request.length() << std::endl;
  return new requestTypes::NetRequest(NULL,0,0,"index.html");

}

